package com.briup.smartlab.web.controller;

import com.briup.smartlab.bean.User;
import com.briup.smartlab.service.IAuthService;
import com.briup.smartlab.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "认证信息管理模块")
@RestController
public class AuthController {
    @Autowired //自动装配
    private IAuthService service; // = new XXX();

    /**
     * 表单数据
     * POST /login
     * 请求头：content-Type application/x-www-form-urlencoded
     * 请求体： username=jack&password=123456
     * @param username
     * @param password
     * @return
     */
    @ApiOperation("登录功能")
    @PostMapping(value = "/login",consumes = "application/x-www-form-urlencoded")
    public Response login(String username,String password){
        //调用service方法
        String token = service.login(username, password);
        return Response.success(token);
    }
    @ApiOperation(value = "获取当前登录的用户",notes = "参数不需要通过手动输入")
    @GetMapping("/auth/me")
    public Response findCurrentUser(HttpServletRequest request){//请求报文-->请求对象
        //从请求头解析jwt字符串，然后查询用户信息，返回结果
        String token = request.getHeader("token");
        User user = service.findByToken(token);
        return Response.success(user);
    }

}
